#include "StdAfx.h"
#include "IAbsDiff.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IAbsDiff::FunctionNameIAbsDiff = "AbsoluteDifference";

IAbsDiff::IAbsDiff(void) 
{
	_fnApi.SetFunctionName(IAbsDiff::FunctionNameIAbsDiff);
	_fnApi.SetFunctionDescription("This function subtracts image from a source image");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Source", "Sets source image", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IAbsDiff::~IAbsDiff(void)
{

}


bool IAbsDiff::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		ProcParamString *ComparisonSrc = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType
		if  (ComparisonSrc != '\0')
		{	
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
			if (input.data != NULL)
			{
				cv::Mat ComparisonImg = cv::imread(ComparisonSrc->GetValueStr());
				if (ComparisonImg.data != NULL)
				{
					cv::Mat output;
					cv::absdiff(input, ComparisonImg, output);
					if (output.data != NULL)
					{
						if (cv::imwrite(step->GetIntermediateImagePathFilename(), output) == true)
							rc = true;
					}
					ComparisonImg.release();
				}
				input.release();
			}
		}
	}
	return rc;
}
